/*
 * yjShare.js
 * 报警共享
 */
;
(function (call) {
	var main = {
		baseUrl: CafeJS.CXT,
		mapId: "jcybMap",
		layerId:"yjxh-layerNew",
        areaCodes:{
            jjj:"11,13",
            hh:"11,13,14,15,37,41"
		},
		init: function () {
			this.initJson();
			this.bindEvt();
			this.queryData();
		},
		initJson: function () {
            var that = this;
            $.ajax({
                url: "/business/jcyb/yjShare/json/yjxh.json",
                dataType: "json",
                async: false,
                success: function (data) {
                    if (!data) return;
                    var arr = data.arr;
                    that.signalMap = [];
                    for (var i = 0; i < arr.length; i++) {
                        that.signalMap[arr[i].code] = arr[i].level;
                    }
                }
            });
		},
		bindEvt: function () {
            var that = this;
            //关闭事件
            $("#modal_yjShare .close").click(function () {
                $("#modal_yjShare").remove();
                $("#yjShare").removeClass("active");
            });
            //点击周边省市
			$(".yjShare-content_item[item='area']").off().on("click","input",function(){
                that.queryData();
			})
			//点击显示等级
            $(".yjShare-content_item[item='level']").off().on("click","input",function(){
                that.queryData();
            })
            
            //点击刷新按钮
            $("#refreshYjShare").off().on("click",function(){
                that.refreshData();
            })
		},
        refreshData:function(){
            var that = this;
           $.ajax({
                url: "/warnclient/updateWarnInfo",
                type: "GET",
                dataType: 'json',
                success: function (res) {
					if(res.success){
						that.queryData();
					}else{
                        alert("刷新失败");
					}
                }
            })  
        },
		getParams: function () {
            var that = this;
            var areaType = [];
            $(".yjShare-content_item[item='level'] input:checked").each(function(index,item){
                areaType.push($(item).val())
            });
            var radioId = $(".yjShare-content_item[item='area'] input:checked").attr("id");
			var obj = {
                areaType:areaType.join(","),
                areaCodes:that.areaCodes[radioId]
			}
			return obj;
		},
		queryData:function(){
            var that = this;
            /*loading.add('#modal_ruler');*/
            if(GLMap.layerIds.indexOf(that.layerId) != -1){
                GIS.LayerControll.deleteLayer(that.mapId, [that.layerId]);
            }
            var obj = that.getParams();
            debugger
            $.ajax({
                url: "/warnclient/queryWarnInfo",
                type: "POST",
                data: obj,
                dataType: 'json',
                success: function (res) {
					if(res.success){
						var data = res.data;
                        var warnIconPath = '/business/jcyb/main/css/images/yjxhicon/';
						var warnCfg = {};
						debugger
                        data.filter(item=>item.eventTypeCode != null).forEach(function (items,index) {
                            warnCfg = {};
                             that.signalMap[items.eventTypeCode].forEach(function (item) {
                                if (item.code == items.severityCode) {
                                    warnCfg = item;
                                    return false;
                                }
                            })
                            if(!$.isEmptyObject(warnCfg)){
                                var picUrl = warnIconPath + warnCfg.limgDown;
                                that.showLayerIcon(that.layerId,{lon:Number(items.lon),lat:Number(items.lat)},picUrl,items);
                            }else{
                                console.log("配置不正确"+JSON.stringify(items));
                            }
                        })
                        GIS.MouseEvent.Bind(that.mapId, that.layerId, null, function(data, id, e){
                            return `<div class="hoverPop"><span>${data.data.headline}</span><span>${data.data.description}</span></div>`;
                        }, null, null, null);

					}else{
                        alert("预警共享失败");
					}
                }
            })
		},
        showLayerIcon: function (layerId, location, iconUrl,data) {
            if(!location.lat || !location.lon){
                return;
            }
            var option = {
                "iconUrl": iconUrl,
                "key": "location",
                "xOffset": 0,
                "yOffset": 0,
                "id": layerId,
                "data": [{
                    "lon": location.lon,
                    "lat": location.lat,
                    "location": "icon",
                    "data":data
                }],
                "iconObj": {
                    "icon": {"x": 0, "y": 0 }
                }
            }
            GIS.Station.showIcons_canvas(this.mapId, option);
        },
	};

	if (typeof call === 'function') {
		call(main);
	}

})(function (main) {
    main.init();
});